home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr37 / satsfaxt.zip / CCPUTILS.ZIP / CCANCEL.C < prev    next >
C/C++ Source or Header  |  1990-01-30  |  12KB  |  283 lines

  1. /*---------------------------------------------------------------------------*
  2.  *                  CCANCEL.C                     *
  3.  *---------------------------------------------------------------------------*
  4.  *  This program allows the user to cancel the current task, as well as      *
  5.  *  pending events.  The action is not immediate; it may take up to         *
  6.  *  thirty seconds for the task to end.                      *
  7.  *                                                                           *
  8.  *  The following source code is intended to assist developers in            *
  9.  *  creating applications which support the  DCA/Intel Communicating         *
  10.  *  Applications Specification Version 1.0A. It is provided free of charge   *
  11.  *  and on an as-is basis. THE IMPLIED WARRENTIES OF MERCHANTABILITY AND     *
  12.  *  FITNESS FOR A PARTICULAR PURPOSE ARE SPECIFICALLY EXCLUDED. This source  *
  13.  *  code may be modified, enhanced, copied and distributed with applications *
  14.  *  that support CAS on a royalty free basis.                                *
  15.  *---------------------------------------------------------------------------*
  16.  *  History:    - Initial release 9/14/89.                     *
  17.  *---------------------------------------------------------------------------*/
  18. #include <stdio.h>
  19. #include <dos.h>
  20. #include <io.h>
  21. #include <malloc.h>
  22. #include "cas.h"            /* Intel CAS header file.             */
  23. #include "parse.h"            /* Parser header file.             */
  24. #include "util.h"            /* CAS utilities header file.         */
  25.  
  26. int JobNo = 0;                /* Cancel specified (pending) job.         */
  27. unsigned long EventNo = 0L;        /* Converted job number.             */
  28. int CancelAll = FALSE;            /* Cancel all pending & current jobs.    */
  29. int Verbose = FALSE;            /* List job#'s as they are cancelled.    */
  30. int Confirm = FALSE;            /* Confirm before cancelling.         */
  31. int Help = FALSE;            /* Show help text.                 */
  32. int RedirOut = FALSE;            /* Redirect output to file.          */
  33.  
  34. CECS *TSTATbuffer;            /* Task Status buffer.             */
  35. ECF *TCFbuffer;             /* Task Control Files buffer.         */
  36. FILE *ccout;                /* Redirected stdout file.             */
  37.  
  38. STABLE SwitchTable[] =            /* Switch table for command line parser. */
  39. {
  40.     { 'A', BOOL,    &CancelAll },
  41.     { 'a', BOOL,    &CancelAll },
  42.     { 'C', BOOL,    &Confirm   },
  43.     { 'c', BOOL,    &Confirm   },
  44.     { 'E', INT,     &JobNo     },
  45.     { 'e', INT,     &JobNo     },
  46.     { 'N', BOOL,    &Verbose   },
  47.     { 'n', BOOL,    &Verbose   },
  48.     { 'R', BOOL,    &RedirOut  },
  49.     { 'r', BOOL,    &RedirOut  },
  50.     { '?', BOOL,    &Help      }
  51. };
  52. #define TABLESIZE (sizeof(SwitchTable)/sizeof(STABLE))
  53.  
  54. char *DescTable[] =            /* Help function description text. */
  55. {
  56.     "CCANCEL (V1.0) Cancels the currently executing event. CCANCEL can\n",
  57.     "also cancel pending events using the /A (all) or /E (single event)\n",
  58.     "options.\n\n",
  59.     "Syntax: CCANCEL [option...]\n\n"
  60. };
  61. #define DESCSIZE (sizeof(DescTable)/sizeof(DescTable[0]))
  62.  
  63. char *HelpTable[] =            /* Help function switch table. */
  64. {
  65.      "/A           Cancel all pending & current event.\n",
  66.      "/E<number>   Cancel specified event.\n\n",
  67.      "/C           Confirm all cancels.\n",
  68.      "/N           Notify by listing events as they're canceled.\n\n",
  69.      "/R           Redirect output to file OUTPUT.CC.\n",
  70.      "/?           Display help information.\n"
  71. };
  72. #define HELPSIZE (sizeof(HelpTable)/sizeof(HelpTable[0]))
  73.  
  74. /*---------------------------------------------------------------------------*
  75.  *                   GetTaskBuffer()                     *
  76.  *---------------------------------------------------------------------------*
  77.  * Get information about the specified task. The TSTATbuffer is filled with  *
  78.  * info about the task on return. The handle returned by the CASOpenTaskFile *
  79.  * is a normal MS-DOS file handle.                         *
  80.  *                                         *
  81.  *---------------------------------------------------------------------------*
  82.  * Parameters:    int Handle - Valid event handle.                 *
  83.  *        BYTE Queue - Which queue to search.                 *
  84.  *        char *TCFbuffer - ECS structure filled with event         *
  85.  *            information.                         *
  86.  *        int *FileHandle - Filled with DOS file handle.             *
  87.  * Return:    none                                 *
  88.  *---------------------------------------------------------------------------*/
  89. GetTaskBuffer(int Handle, BYTE Queue, char *TCFbuffer, int *FileHandle)
  90. {
  91.     /* Open the Task Control File and read information. */
  92.     if((*FileHandle = CASOpenFile(Handle,0,Queue)) > 0)
  93.         read(*FileHandle, TCFbuffer, sizeof(ECF));
  94.     else
  95.     CASError(CASNOTCF, TRUE, -(*FileHandle));
  96. }
  97.  
  98. /*---------------------------------------------------------------------------*
  99.  *                   CancelAllJobs()                     *
  100.  *---------------------------------------------------------------------------*
  101.  * Cancel all tasks that are pending. If confirmation is asked on the command*
  102.  * line, then query the user before canceling. The verbose option prints a   *
  103.  * message including the job number but does not query the user before         *
  104.  * canceling.                                     *
  105.  *---------------------------------------------------------------------------*
  106.  * Parameters:    ECF *TCFbuffer - Unintialized ECF structure.             *
  107.  * Return:    none                                 *
  108.  *---------------------------------------------------------------------------*/
  109. CancelAllJobs(ECF *TCFbuffer)
  110. {
  111.     int EventHandle;            /* Event handle to cancel.             */
  112.  
  113.     /* Get the first event in the task queue. */
  114.     EventHandle = CASFindFirst(ANY_STATUS, SEARCH_FORWARD, TASK_QUEUE);
  115.  
  116.     while(EventHandle > 0) {
  117.     /* Cancel the event. */
  118.     CancelJob(EventHandle, TCFbuffer);
  119.  
  120.     /* Now get the next event in the task queue. */
  121.     EventHandle = CASFindNext(TASK_QUEUE);
  122.     }
  123. }
  124.  
  125. /*---------------------------------------------------------------------------*
  126.  *                  CancelJob()                     *
  127.  *---------------------------------------------------------------------------*
  128.  * Cancel the specified (pending) job. This is accomplished by simply         *
  129.  * deleting the Task Control File. If confirmation is asked on the command   *
  130.  * line, then query the user before canceling. The verbose option prints     *
  131.  * a message including the job number, but does not query the user before    *
  132.  * canceling.                                     *
  133.  *---------------------------------------------------------------------------*
  134.  * Parameters:    int EventHandle - Event handle of job to cancel.         *
  135.  *        ECF *TCFbuffer - Uninitialized ECF structure.             *
  136.  * Return:    none                                 *
  137.  *---------------------------------------------------------------------------*/
  138. CancelJob(int EventHandle, ECF *TCFbuffer)
  139. {
  140.  
  141.     int ErrorCode;            /* Error returned from CAS function.     */
  142.     int FileHandle;            /* DOS file handle (from GetTaskBuffer). */
  143.     char answer[1];            /* Users response to confirmation quest. */
  144.  
  145.     /* if requested, be verbose/confirm. */
  146.     if(Verbose | Confirm) {
  147.         /* Get information about the task and close it. */
  148.         GetTaskBuffer(EventHandle, TASK_QUEUE, (char *)TCFbuffer, &FileHandle);
  149.         close(FileHandle);
  150.  
  151.     fprintf(stdout, "Event Number %d.\n", EventHandle);
  152.         if(Confirm) {
  153.         fprintf(stdout, "         Cancel event (y/n)? ");
  154.             gets(answer);
  155.         }
  156.  
  157.         if((Confirm && (toupper(answer[0]) == 'Y')) || (!Confirm))
  158.         /* Delete the control file, this will in effect cancel the job. */
  159.             if((ErrorCode = CASDeleteFile(EventHandle,0,TASK_QUEUE)) == SUCCESS)
  160.         fprintf(stdout, "Pending event canceled.\n");
  161.             else
  162.         CASError(CASNOCAN, TRUE, -ErrorCode);
  163.         }
  164.     else
  165.     /* Delete the control file. */
  166.         if((ErrorCode = CASDeleteFile(EventHandle,0,TASK_QUEUE)) == SUCCESS)
  167.             ;
  168.         else
  169.         CASError(CASNOCAN, TRUE, -ErrorCode);
  170. }
  171.  
  172. /*---------------------------------------------------------------------------*
  173.  *                 CancelCurrent()                     *
  174.  *---------------------------------------------------------------------------*
  175.  * Cancels the currently executing event. If confirmation is asked on the    *
  176.  * command line, query the user before canceling. The verbose option prints  *
  177.  * a message but does not query the user before canceling.             *
  178.  *---------------------------------------------------------------------------*
  179.  * Parameters:    CECS *TSTATbuffer - An uninitialized CECS structure.         *
  180.  * Return:    None                                 *
  181.  *---------------------------------------------------------------------------*/
  182. CancelCurrent(CECS *TSTATbuffer)
  183. {
  184.     int ErrorCode;            /* Error returned from CAS function.     */
  185.     int EventHandle;            /* Event handle (from GetCurrentEvent).  */
  186.     char answer[1];            /* Users response to confirmation quest. */
  187.  
  188.     /* Get information about the task. The TSTATbuffer is a structure
  189.      * containing the Task Control File record and ONE File Transfer Record.
  190.      * This call is made to retrieve the EventHandle of the currently executing
  191.      * event. */
  192.     if(EventHandle = CASGetCurrentEventStatus(TSTATbuffer) > 0) {
  193.         if(Verbose | Confirm) {
  194.         fprintf(stdout, "Canceling current event.\n", EventHandle);
  195.             if(Confirm) {
  196.         fprintf(stdout, "         Cancel job (y/n)? ");
  197.                 gets(answer);
  198.         }
  199.             if((Confirm && (toupper(answer[0]) == 'Y')) || (!Confirm))
  200.         /* Cancel the current event as specified by EventHandle. */
  201.         if((ErrorCode = CASAbortCurrentEvent()) > 0)
  202.             fprintf(stderr, "Current event canceled.\n");
  203.         else
  204.             CASError(CASNOCUR, TRUE, -ErrorCode);
  205.     }
  206.         else
  207.         /* Cancel the current event. */
  208.         if((ErrorCode = CASAbortCurrentEvent()) > 0)
  209.         ;
  210.         else
  211.         CASError(CASNOCUR, TRUE, -ErrorCode);
  212.     }
  213.     else
  214.     /* If CASGetCurrentEventStatus returns FAIL, then no current event! */
  215.     if(!CancelAll)
  216.         CASError(CASNOEVENT, TRUE, 0);
  217. }
  218.  
  219. /*---------------------------------------------------------------------------*
  220.  *                  MAIN()                     *
  221.  *---------------------------------------------------------------------------*
  222.  * Prepare to cancel the current event. Two buffers need to be created,      *
  223.  * TSTAT and TCF. If a job number is specified on the command line, then     *
  224.  * cancel that job. Otherwise, cancel the current job (this is the default). *
  225.  * If /A given, then cancel all current and pending jobs.             *
  226.  *---------------------------------------------------------------------------*
  227.  *  Return: The DOS exit code is 0 if no errors are encountered, 1 for         *
  228.  *        commandline syntax errors, 2 for CCAM error, and 3 for DOS         *
  229.  *        errors.                                 *
  230.  *---------------------------------------------------------------------------*/
  231. main(int argc, char **argv)
  232. {
  233.     int result;             /* DOS & CAS function return value.      */
  234.  
  235.     argc = ParseCommand(argc, argv, SwitchTable, TABLESIZE);
  236.  
  237.     /* Convert job number to long. */
  238.     EventNo = atol(JobNo);
  239.  
  240.     /* If help is wanted, print info and exit (with 0 exit code). */
  241.     if(Help)
  242.     CASHelp(DescTable, DESCSIZE, HelpTable, HELPSIZE);
  243.  
  244.     /* Insure that the Resident Scheduler is installed. */
  245.     if((result = CASGetInstalledState()) != INSTALLED) {
  246.     if( result == NOTiOK )
  247.         CASError(CASNOHWO, TRUE, 0);
  248.     else
  249.         CASError(CASNOHWN, TRUE, 0);
  250.     }
  251.  
  252.     /* Redirect stdout to a file. */
  253.     if(RedirOut) {
  254.     if((ccout = fopen("OUTPUT.CC", "w")) == NULL)
  255.         CASError(CASOPENTEMP, TRUE, 0);
  256.     if(-1 == dup2(fileno(ccout), 1))
  257.         CASError(CASBADREDIR, TRUE, 0);
  258.     }
  259.  
  260.     /* Get buffer for Task information. */
  261.     TSTATbuffer = (CECS *)malloc(sizeof(CECS));
  262.     if(TSTATbuffer == NULL)
  263.     CASError(CASNOMEM, TRUE, 0);
  264.  
  265.     /* Get buffer for Task Control information. */
  266.     TCFbuffer = (ECF *)malloc(sizeof(ECF));
  267.     if(TCFbuffer == NULL)
  268.     CASError(CASNOMEM, TRUE, 0);
  269.  
  270.     /* Cancel the specified job or the current event if none was entered
  271.      * on command line. */
  272.     if(JobNo != 0)
  273.     CancelJob(JobNo, TCFbuffer);
  274.     else if(CancelAll) {
  275.     CancelAllJobs(TCFbuffer);
  276.     CancelCurrent(TSTATbuffer);
  277.     }
  278.     else
  279.     CancelCurrent(TSTATbuffer);
  280.  
  281.     exit(0);
  282. }
  283.